package com.study.controller;

import com.study.domain.User;
import com.study.exception.SysException;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView() {
        User user = new User();
        user.setUname("小美");
        user.setAge(22);

        ModelAndView mv = new ModelAndView();
        mv.addObject("user", user);
        mv.setViewName("success");
        return mv;
    }

    @RequestMapping("/testAjax")
    public @ResponseBody
    User testAjax() {
        User user = new User();
        user.setUname("小美");
        user.setAge(22);
        System.out.println(user);
        return user;
    }

    //传统方式接收图片
    @RequestMapping("fileUpLoad1")
    public String fileUpLoad1(HttpServletRequest request) throws Exception {
        //上传的位置
        String path = request.getSession().getServletContext().getRealPath("/uploads/");
        File file = new File(path);
        //判断路径是否存在并创建
        System.out.println("path:" + path);
        if (!file.exists()) {
            file.mkdirs();
        }
        //解析request对象，获取上传文件项
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        //解析request
        List<FileItem> items = upload.parseRequest(request);
        //遍历
        for (FileItem item : items) {
            //进行判断，当前item是否是上传文件项
            if (item.isFormField()) {
                //说明是普通表单
            } else {
                //说明是文件上传
                //获取到上传文件的名称
                String filename = item.getName();
                item.write(new File(path, filename));
                //删除临时文件
                item.delete();
            }

        }
        return "success";
    }

    @RequestMapping("fileUpLoad2")
    public String fileUpLoad2(HttpServletRequest request, MultipartFile upload) throws Exception {
        //上传的位置
        String path = request.getSession().getServletContext().getRealPath("/uploads/");
        File file = new File(path);
        //判断路径是否存在并创建
        System.out.println("mvc path:" + path);
        if (!file.exists()) {
            file.mkdirs();
        }
        String filename = upload.getOriginalFilename();
        upload.transferTo(new File(path,filename));
        return "success";
    }

    @RequestMapping("/testException")
    public String testException() throws SysException {
        try {
            int a = 10/0;
            System.out.println(a);
        } catch (Exception e){
            e.printStackTrace();
            throw new SysException("异常发生了");
        }
        return "success";
    }
    @RequestMapping("/testInterceptor")
    public String testInterceptor() throws SysException {
        try {
            int a = 10/0;
            System.out.println(a);
        } catch (Exception e){
            e.printStackTrace();
            throw new SysException("异常发生了");
        }
        return "success";
    }
}
